package br.com.almoxarifadoporttek.tools;

import org.apache.commons.mail.*;



public class EmailUtils {
	
	 	private static final String HOSTNAME = "smtp.dsptelecom.com.br";	
	  private static final String USERNAME = "dsptelecom@dsptelecom.com.br";	
	  private static final String PASSWORD = "segundadivisao13";	
	  private static final String EMAILORIGEM = "dsptelecom@dsptelecom.com.br";
	  private static final String USERORIGEM = "DSP Telecom";
	
	  public static Email conectaEmail() throws EmailException
	  {	
		  Email email = new SimpleEmail();	 
		  email.setHostName(HOSTNAME);	
		  email.setSmtpPort(25);
		  email.setAuthentication(USERNAME, PASSWORD); 
		  email.setTLS(true);	
		  email.setFrom(EMAILORIGEM);	
		  return email;
	 
	  }
	
	  
	
	  public static void enviaEmail( String destino, String titulo, String mensagem) throws EmailException
	  {
		  
		  
		  try
		  {
		  
			SimpleEmail email = new SimpleEmail();  
	        email.setHostName(HOSTNAME);  
	        email.setAuthentication(USERNAME, PASSWORD);  
	        email.setSSL(true);  
            email.setTLS(true); 
	        email.setSmtpPort(465);
	        email.addTo(destino);  
	        email.setFrom(EMAILORIGEM);  
	        email.setSubject(titulo);  
	        email.setMsg(mensagem);  
	        email.send();  
	        email = null; 
	
	        
		  }
		  catch(Exception e)
		  {
			  System.out.println("Email error : "+e);
		  }
	        
	
	
	  }
	  
	  
	  public void enviaEmail2(String emailDestino,String nomeDestino, String titulo, String mensagem) 
	  {
		  
		  
		  try 
		  {
		  
			  Email email = new SimpleEmail();
	
			  email.setSmtpPort(25);
			  email.setAuthenticator(new DefaultAuthenticator(USERNAME, PASSWORD));
			  email.setDebug(true);
			  email.setHostName(HOSTNAME);			 
			  email.getMailSession().getProperties().put("mail.smtps.auth", "true");			
			  email.getMailSession().getProperties().put("mail.debug", "true");
			  email.getMailSession().getProperties().put("mail.smtps.port", "25");
			  email.getMailSession().getProperties().put("mail.smtps.socketFactory.port", "25");
			  email.getMailSession().getProperties().put("mail.smtps.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
			  email.getMailSession().getProperties().put("mail.smtps.socketFactory.fallback", "false");
			  email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
			  email.setFrom(EMAILORIGEM, USERORIGEM);
			  email.setSubject(titulo);
			  email.setMsg(mensagem);
			  email.addTo(emailDestino, nomeDestino);
			  email.setTLS(true);
			  email.setSSL(true); 
			  email.send();
		  
		  } catch (EmailException e) {
				System.out.print("Error. :");
				e.printStackTrace();
			}
	  }


}
